package org.firebug.spring.boot.controller;

/**
 * Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线，保证数据能按照指定的方式进行传输,
 * Exchange：消息交换机,它指定消息按什么规则,路由到哪个队列。
 * Queue:消息的载体,每个消息都会被投到一个或多个队列。
 * Binding:绑定，它的作用就是把exchange和queue按照路由规则绑定起来.
 * Routing Key:路由关键字,exchange根据这个关键字进行消息投递。
 * vhost:虚拟主机,一个broker里可以有多个vhost，用作不同用户的权限分离。
 * Producer:消息生产者,就是投递消息的程序.
 * Consumer:消息消费者,就是接受消息的程序.
 * Channel:消息通道,在客户端的每个连接里,可建立多个channel.
 */
//@Configuration
public class RabbitmqConfig {
//    public static final String EXCHANGE = "felix.exchange";
//    public static final String ROUTING_KEY = "felix.routing.key";
//    public static final String QUEUE_NAME = "felix.queue.name";
//    private final Logger logger = LoggerFactory.getLogger(this.getClass());
//
//    @Autowired
//    private CachingConnectionFactory connectionFactory;
//    @Autowired
//    private SimpleRabbitListenerContainerFactoryConfigurer factoryConfigurer;
//
//    @Bean(name = {"connectionFactory"})
//    public CachingConnectionFactory connectionFactory() {
//        CachingConnectionFactory factory = new CachingConnectionFactory();
//        factory.setHost("api.firebug.org");
//        factory.setPort(5672);
//        factory.setUsername("admin");
//        factory.setPassword("123456");
//        factory.setVirtualHost("/");
//        factory.setPublisherConfirms(true); // 消息确认：rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {}
////        factory.setPublisherReturns(true);
//        return factory;
//    }
//
//    @Bean(name = "listenerContainerFactory")
//    public SimpleRabbitListenerContainerFactory listenerContainerFactory() {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        factoryConfigurer.configure(factory, connectionFactory);
//        factory.setMessageConverter(new Jackson2JsonMessageConverter());
//        factory.setConcurrentConsumers(1);
//        factory.setMaxConcurrentConsumers(1);
//        factory.setPrefetchCount(1);
//        factory.setTxSize(1);
//        factory.setAcknowledgeMode(AcknowledgeMode.NONE);
//        return factory;
//    }
//
//    @Bean
//    public RabbitTemplate rabbitTemplate() {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
////        rabbitTemplate.setMandatory(true);
////        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
////        rabbitTemplate.setRecoveryCallback(new RecoveryCallback() {
////            @Override
////            public Object recover(RetryContext retryContext) throws Exception {
////                logger.info("RecoveryCallback: retryContext({})", retryContext);
////                return retryContext;
////            }
////        });
////        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
////            @Override
////            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
////                if (ack) {
////                    logger.info("ConfirmCallback OK: correlationData({}),ack({}),cause({})", correlationData, ack, cause);
////                } else {
////                    logger.info("ConfirmCallback NG: correlationData({}),ack({}),cause({})", correlationData, ack, cause);
////                }
////            }
////        });
////        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
////            @Override
////            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
////                String body = new String(message.getBody());
////                logger.info("ReturnCallback: message.body:({}),replyCode({}),replyText({}),exchange({}),routingKey({}),", body, replyCode, exchange, replyText, routingKey);
////            }
////        });
//        return rabbitTemplate;
//    }
//
//    @Bean
//    public Queue queue() {
//        return new Queue(QUEUE_NAME, true, false, false, null);
//    }
//
//    @Bean
//    public DirectExchange exchange() {
//        return new DirectExchange(EXCHANGE, true, false);
//    }
//
//    @Bean
//    public Binding building() {
//        return BindingBuilder.bind(queue()).to(exchange()).with(ROUTING_KEY);
//    }
}